class Solution
{
public:
    int kthSmallest(TreeNode* root, int k)
    {
        int res;
        dfs(root, k, res);
        return res;
    }

private:
    void dfs(TreeNode* root, int& k, int& res)
    {
        if (root == nullptr)   return;

        dfs(root->left, k, res);

        if (k == 0)    return;
        if (--k == 0)
        {
            res = root->val;
            return;
        }
        dfs(root->right, k, res);
    }
};